import mimetypes
from datetime import datetime

from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, Http404, HttpResponse, HttpResponseNotFound
from django.template import RequestContext
from django.db.models import Q

from django.views.decorators.http import condition

from assets.search import searchAssets  
from tags.views import allTags

from damn import TranscoderInstance

from assets.models import *

from blogs.models import Entry

from favorites.models import *

from django.contrib.comments.models import Comment

def index(request):	
  if request.user.is_authenticated():
    assets = Asset.objects.with_favorite_for(request.user)
  else:
    assets = Asset.objects
  assets = assets.exclude(format__name='texture').order_by('-stamp')[:15]
  comments = Comment.objects.order_by('submit_date')[:10]
  favorites = Favorite.objects.favorites_for_model(Asset).order_by('-created')[:10]
  blogs = Entry.objects.filter(author__is_staff=True).order_by('-pub_date')[:10]
  return render_to_response('index.html', locals(), context_instance=RequestContext(request))

def asset(request, as_id):	
  asset = get_object_or_404(Asset, id=as_id)
  author = asset.file.author if asset.file else None
  formats = TranscoderInstance().formats_clean(asset.format.name)
  #tags = allTags()
  favoreds = Favorite.objects.favorites_for_object(asset)
  resource = asset
  return render_to_response('asset.html', locals(), context_instance=RequestContext(request))


def file(request, file_id):	
  file = get_object_or_404(File, id=file_id)
  author = file.author
  resource = file
  return render_to_response('file.html', locals(), context_instance=RequestContext(request))
  

def searcher(request):
  assets = Asset.objects.exclude(format__name='texture').order_by('-stamp')[:15]
  tags = allTags()
  formats = FormatType.objects.values('id', 'name').distinct()
  return render_to_response('search.html', locals(), context_instance=RequestContext(request))  
  
  
def search(request):
  assets, total = searchAssets(request)
  tags = allTags()
  formats = FormatType.objects.values('id', 'name').distinct()
  return render_to_response('search.html', locals(), context_instance=RequestContext(request))  
  
  
